package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.RankSong;
import com.javaclimb.music.service.RankSongService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 榜单的歌曲管理controller
 */
@RestController
@RequestMapping("/rankSong")
public class RankSongController {

    @Autowired
    private RankSongService rankSongService;

    /**
     * 给榜单添加歌曲
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object addListSong(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        //获取前端传来的参数
        String songId = request.getParameter("songId").trim();  //歌曲id
        String rankId = request.getParameter("rankId").trim(); //榜单id
        RankSong rankSong = new RankSong();
        rankSong.setSongId(Integer.parseInt(songId));
        rankSong.setRankId(Integer.parseInt(rankId));
        boolean flag = rankSongService.insert(rankSong);
        if(flag){
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"保存成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"保存失败");
        return jsonObject;

    }

    /**
     * 根据榜单id查询歌曲
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public Object detail(HttpServletRequest request){
        String rankId = request.getParameter("rankId");
        return rankSongService.rankSongOfRankId(Integer.parseInt(rankId));
    }

    /**
     * 删除榜单里的歌曲
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Object delete(HttpServletRequest request){
        String songId = request.getParameter("songId").trim();                 //歌曲id
        String rankId = request.getParameter("rankId").trim();        //榜单id
        boolean flag = rankSongService.deleteBySongIdAndRankId(Integer.parseInt(songId),Integer.parseInt(rankId));
        return flag;
    }

}




















